apiready = function() {

    j = 0;
    arr = new Array();
    arr1 = new Array();
    var arr2 = new Array(); //我的页面用来存储帖子id的数组
    var post_user_avater = $api.byId('post_user_avater');
    var post_username = $api.byId('post_username');
    var post_goods_pic = $api.byId('post_goods_pic');
    var post_note_count = $api.byId('post_note_count');
    var post_laud_count = $api.byId('post_laud_count');
    var post_star_count = $api.byId('post_star_count');
    var post_goods_content = $api.byId('post_goods_content');
    var post_state = $api.byId('post_state');

    var aFooterList = $api.domAll('#tab .aui-tab-item');
    var changeIndexFrame = function(index) {
        for (var i = 0; i < 4; i++) {
            if (i == index) {
                $api.addCls(aFooterList[index], 'aui-active');
            } else {
                $api.removeCls(aFooterList[i], 'aui-active');
            }
        };
    };

    /* 当刚进有圈时，不点击导航栏也可做下拉和上拉刷新 */
    // var cou=api.pageParam.cou;
    // if(cou==1){
    //   refresh(post_list);
    //   cou=0;
    // }
    /* end */


    window.Select = function() {
        var token = $api.getStorage('token');
        if (token == '') {
            api.alert({
                msg: '您还未登录，请先登录！'
            })
        }
        var body = {
            categry: 0,
            token: token
        }
        categryAJAX(body);
        refresh(post_list);
        changeIndexFrame(0);
    }

    window.Focus = function() {
        var token = $api.getStorage('token');
        if (token == '') {
            api.alert({
                msg: '您还未登录，请先登录！'
            })
        }
        var body = {
            categry: 1,
            token: token
        }
        categryAJAX(body);
        // refresh(categryAJAX, body);
        changeIndexFrame(1);
    }

    window.openPostInfo = function(tieid, id) {
        api.openFrame({
            name: 'postInfo',
            url: '../youquan/postInfo.html',
            rect: {
                x: 0,
                y: 0,
                w: 'auto',
                h: 'auto'
            },
            pageParam: {
                postinfo: tieid,
                id: id
            },
            allowEdit: true
        });
    }
    window.openPublish = function() {
        var token = $api.getStorage('token');
        if (token == '') {
            api.alert({
                msg: '您还未登录，不能发表帖子',
            }, function(ret, err) {});
        } else {
            api.openFrame({
                name: 'publishPost',
                url: '../youquan/publishPost.html',
                rect: {
                    x: 0,
                    y: 0,
                    w: 'auto',
                    h: 'auto'
                },
                allowEdit: true
            });
            changeIndexFrame(3);
        }
    }
    api.addEventListener({
        name: 'publish'
    }, function(ret, err) {
        if (ret) {
            changeIndexFrame(ret.value.index);
            Mine();
        } else {
            // alert(JSON.stringify(err));
        }
    });

    /* 下拉和上拉刷新函数 */
    function refresh(func, con) {
        api.addEventListener({
            name: 'scrolltobottom'
        }, function(ret, err) {
            $api.byId('refresh').style = "display:block";
            var token = {
                j: ++j
            }
            api.ajax({
                url: 'http://www.mk517.com/webApi/youquans.php',
                tag: 'refresh',
                method: 'post',
                dataType: 'json',
                "headers": {
                    "Content-Type": "application/json"
                },
                data: {
                    body: token
                }
            }, function(ret, err) {
                $api.byId('refresh').style = "display:none";
                func(con);

                if (j > 0) {
                    api.addEventListener({
                        name: 'swipedown'
                    }, function(ret, err) {
                        $api.byId('refresh').style = "display:block";
                        var body = {
                            j: --j
                        }
                        api.ajax({
                            url: 'http://www.mk517.com/webApi/youquans.php',
                            tag: 'refresh',
                            method: 'post',
                            dataType: 'json',
                            "headers": {
                                "Content-Type": "application/json"
                            },
                            data: {
                                body: body
                            }
                        }, function(ret, err) {
                            $api.byId('refresh').style = "display:none";
                            func(con);
                        });

                    });
                }

            });

        });
    }
    /* 上拉和下拉刷新结束 */


    function post_list() {
        var body = {
            token: $api.getStorage('token'),
            j: j
        };
        api.ajax({
            url: 'http://www.mk517.com/webApi/youquans.php',
            method: 'post',
            "headers": {
                "Content-Type": "application/json"
            },
            dataType: 'json',
            data: {
                body: body
            }
        }, function(ret, err) {
            //alert(JSON.stringify(ret));
            if (ret) {
                if (!ret.info && ret.length > 0) {
                    $api.byId('content').innerHTML = '';
                    for (var i = 0; i < ret.length; i++) {
                        data = ret[i];
                        str = 'change_status' + i;
                        arr.push(data.id);
                        arr1.push(data.tieid);
                        laud = 'laud' + i;
                        str1 = 'hotIcon' + i
                        data.post_state % 2 == 0 ? status = '关注' : status = '已关注';
                        (data.post_picture != null && data.post_picture != '') ? (imgAddress = '<div style="width:auto;height:auto;" onclick="openPostInfo(' + data.tieid + ',' + data.id + ')"><img src="' + data.post_picture + '" id="picture1" style="width:100%;height:auto;max-width:100%;max-height:100%;"></div>') : imgAddress = '';
                        $api.byId('content').innerHTML +=
                            '<div class="aui-card-list aui-content">' +
                            '<div class="aui-card-list-header aui-card-list-user aui-border-b">' +
                            '<div class="aui-card-list-user-avatar">' +
                            '<img id="post_user_avater" src="' + data.post_user_avater + '" class="aui-img-round" />' +
                            '</div>' +
                            '<div class="aui-card-list-user-name">' +
                            '<div id="post_username">' + data.post_username + '<i id="' + str1 + '" class="icon iconfont" style="color:red;"></i></div>' +
                            '<span class="aui-btn aui-btn-warning" id="' + str + '" onclick="change(' + i + ',' + data.id + ');">' + status + '</span>' +
                            '</div>' +
                            '</div>' +
                            '<div class="aui-card-list-content-padded">' +
                            '<p id="post_goods_content" onclick="openPostInfo(' + data.tieid + ',' + data.id + ')">' + data.post_goods_content + '</p>' +
                            imgAddress +
                            '</div>' +
                            '<div class="aui-card-list-footer aui-border-t">' +
                            '<div id="post_note_count"><i class="aui-iconfont aui-icon-note" id="note" onclick="postComment(' + data.tieid + ',' + data.id + ')">' + data.post_note_count + '</i></div>' +
                            '<div id="post_laud_count"><i id="' + laud + '" class="aui-iconfont aui-icon-laud" onclick="postLaud(' + i + ',' + data.tieid + ')">' + data.post_laud_count + '</i></div>' +
                            '<div id="post_star_count"><i class="aui-iconfont aui-icon-share" onclick="sharePost(' + i + ');"></i>' +
                            '</div>' +
                            '<div id="post_star_count"><i class="icon iconfont icon-hongbao" onclick="postMon(' + data.id + ',' + data.tieid + ')">0</i>' +
                            '</div>' +
                            '</div></div></div>';
                        if (data.host == 1) {
                            $api.addCls($api.byId(str1), 'icon-huo')
                        };
                        if (status == "已关注") {
                            $api.removeCls($api.byId(str), 'aui-btn-warning');
                        };
                        // if (data.post_picture != '') {
                        //     $api.byId('picture1').style = 'display:block';
                        // } else {
                        //     $api.byId('picture1').style = 'display:none';
                        // };
                    }
                }
                // changeIndexFrame(0);
                // alert(JSON.stringify(ret));
            } else {
                //alert(JSON.stringify(err));
            }
        });
    }
    post_list();


    /*
     *   添加事件监听未发表帖子时，关闭窗口时的跳转
     */
    addCloseEvent()

    function addCloseEvent() {
        api.addEventListener({
            name: 'closePublish'
        }, function(ret, err) {
            if (ret) {
                var eve = ret.value.sta_close;
                if (eve == 1) {
                    post_list();
                    changeIndexFrame(0);
                }
                //  alert( JSON.stringify( ret ) );
            } else {
                //  alert( JSON.stringify( err ) );
            }
        });

    }

    /* 添加监听事件结束 */

    var s = 0;
    window.change = function(i, userid) {
        var token = $api.getStorage('token');
        var body = {
            token: token,
            id: userid,
            sta: s++
        };
        api.ajax({
            url: 'http://www.mk517.com/webApi/follow.php',
            method: 'post',
            dataType: 'json',
            "headers": {
                "Content-Type": "application/json"
            },
            data: {
                body: body
            }
        }, function(ret, err) {
            if (ret) {
                if (ret.info == 'true') {
                    //  alert(status);
                    if (status == '关注') {
                        status = '已关注';
                        var str = 'change_status' + i;
                        $api.byId(str).innerHTML = status;
                        $api.removeCls($api.byId(str), 'aui-btn-warning');
                    } else {
                        status = '关注';
                        var str = 'change_status' + i;
                        $api.byId(str).innerHTML = status;
                        $api.addCls($api.byId(str), 'aui-btn-warning');
                    }
                } else {
                    api.alert({
                        msg: '关注失败',
                    }, function(ret, err) {});
                }
                // alert( JSON.stringify( ret ) );
            } else {
                //  alert(JSON.stringify(err));
            }
        });

    }

    function categryAJAX(body) {

        api.ajax({
            url: 'http://www.mk517.com/webApi/youquans.php',
            method: 'post',
            dataType: 'json',
            "headers": {
                "Content-Type": "application/json"
            },
            data: {
                body: body
            }
        }, function(ret, err) {
            //alert(JSON.stringify(ret));
            if (ret) {
                if (ret.length > 0) {
                    $api.byId('content').innerHTML = '';
                    for (var i = 0; i < ret.length; i++) {
                        data = ret[i];
                        str = 'change_status' + i;
                        arr.push(data.id);
                        arr1.push(data.tieid);
                        laud = 'laud' + i;
                        str1 = 'hotIcon' + i;
                        data.post_state % 2 == 0 ? status = '关注' : status = '已关注';
                        (data.post_picture != null && data.post_picture != '') ? (imgAddress = '<div style="width:auto;height:auto;" onclick="openPostInfo(' + data.tieid + ',' + data.id + ')"><img src="' + data.post_picture + '" id="picture1" style="width:100%;height:auto;max-width:100%;max-height:100%;"></div>') : imgAddress = '';
                        $api.byId('content').innerHTML +=
                            '<div class="aui-card-list">' +
                            '<div class="aui-card-list-header aui-card-list-user aui-border-b">' +
                            '<div class="aui-card-list-user-avatar">' +
                            '<img id="post_user_avater" src="' + data.post_user_avater + '" class="aui-img-round" />' +
                            '</div>' +
                            '<div class="aui-card-list-user-name">' +
                            '<div id="post_username">' + data.post_username + '<i id="' + str1 + '" class="icon iconfont" style="color:red;"></i></div>' +
                            '<span class="aui-btn aui-btn-warning" id="' + str + '" onclick="change(' + i + ',' + data.id + ');">' + status + '</span>' +
                            '</div>' +
                            '</div>' +
                            '<div class="aui-card-list-content-padded">' +
                            '<p id="post_goods_content" onclick="openPostInfo(' + data.tieid + ',' + data.id + ')">' + data.post_goods_content + '</p>' +
                            imgAddress +
                            '</div>' +
                            '<div class="aui-card-list-footer aui-border-t">' +
                            '<div id="post_note_count"><i class="aui-iconfont aui-icon-note" id="note" onclick="postComment(' + data.tieid + ',' + data.id + ')">' + data.post_note_count + '</i></div>' +
                            '<div id="post_laud_count"><i id="' + laud + '" class="aui-iconfont aui-icon-laud" onclick="postLaud(' + i + ',' + data.tieid + ')">' + data.post_laud_count + '</i></div>' +
                            '<div id="post_star_count"><i class="aui-iconfont aui-icon-share" onclick="sharePost(' + i + ');"></i>' +
                            '</div>' +
                            '<div id="post_star_count"><i class="icon iconfont icon-hongbao" onclick="postMon(' + data.id + ',' + data.tieid + ')">0</i>' +
                            '</div>' +
                            '</div></div></div>';
                     
                    }
                }
            } else {
                //alert(JSON.stringify(err));
            }
        });
    };


    window.postComment = function(tieid, id) {
        api.prompt({
            title: '请输入您的评论：',
            buttons: ['确定', '取消']
        }, function(ret, err) {
            var index = ret.buttonIndex;
            var text = ret.text;
            var token = $api.getStorage('token');
            if (index == 1) {
                var body = {
                    token: token,
                    msg: text,
                    tieid: tieid,
                    id: id,
                    comm_id: 0,
                    comm_uid: 0
                };
                api.ajax({
                    url: 'http://www.mk517.com/webApi/comment.php',
                    method: 'post',
                    dataType: 'json',
                    "headers": {
                        "Content-Type": "application/json"
                    },
                    data: {
                        body: body
                    }
                }, function(ret, err) {
                    if (ret) {
                        if (ret.info == "true") {
                            $api.byId('note').innerHTML = ret.content;
                            api.openFrame({
                                name: 'postInfo',
                                url: '../youquan/postInfo.html',
                                rect: {
                                    x: 0,
                                    y: 0,
                                    w: 'auto',
                                    h: 'auto'
                                },
                                pageParam: {
                                    postinfo: tieid,
                                    id: id
                                },
                                allowEdit: true
                            });
                        } else {
                            api.alert({
                                msg: '评论失败，请重试！',
                            }, function(ret, err) {});

                        }
                        // alert(JSON.stringify(ret));
                    } else {
                        //  alert(JSON.stringify(err));
                    }
                });

            }
        });
    };
    /*
     *点赞功能start
     */
    window.postLaud = function(i, tieid) {
        var token = $api.getStorage('token');
        if (token != '') {
            if ($api.getStorage('state')) {
                var token = $api.getStorage('token');
                var body = {
                    token: token,
                    tieid: tieid,
                    sta: 1
                };
                cancelLaudAjax(body, i);
                $api.rmStorage('state');
            } else {
                var token = $api.getStorage('token');
                var body = {
                    token: token,
                    tieid: tieid,
                    sta: 0
                }
                laudAjax(body, i);
            }
        } else {
            api.alert({
                msg: '您还没有登录，请先登录！',
            }, function(ret, err) {});

        }
    };

    function laudAjax(body, i) {
        api.ajax({
            url: 'http://www.mk517.com/webApi/like.php',
            method: 'post',
            dataType: 'json',
            "headers": {
                "Content-Type": "application/json"
            },
            data: {
                body: body
            }
        }, function(ret, err) {
            alert(JSON.stringify(ret));
            if (ret) {
                if (ret.state == 'true') {
                    $api.setStorage('state', ret.state);
                    var laud = 'laud' + i;
                    $api.byId(laud).style.color = '#0000E3';
                    $api.byId(laud).innerHTML = (ret.count < 0) ? 0 : ret.count;
                }
                //alert(JSON.stringify(ret));
            } else {
                //alert(JSON.stringify(err));
            }
        });
    }

    function cancelLaudAjax(body, i) {
        api.ajax({
            url: 'http://www.mk517.com/webApi/like.php',
            method: 'post',
            dataType: 'json',
            "headers": {
                "Content-Type": "application/json"
            },
            data: {
                body: body
            }
        }, function(ret, err) {
            if (ret) {
                var laud = 'laud' + i;
                $api.byId(laud).style.color = '';
                $api.byId(laud).innerHTML = (ret.count < 0) ? 0 : ret.count;
            } else {
                //alert(JSON.stringify(err));
            }
        });

    }
    /*
     *点赞功能end
     */

    window.sharePost = function(i) {
        var shareMdl = api.require('shareAction');
        shareMdl.share({
            text: '',
            type: 'text'
        });
    };

    /* 打赏功能start */
    moneyOfCount = 0;

    function ajaxOfjifen() {
        var token = $api.getStorage('token');
        var body = {
            token: token
        }
        api.ajax({
            url: 'http://www.mk517.com/webApi/jifen.php',
            method: 'post',
            dataType: 'json',
            "headers": {
                "Content-Type": "application/json"
            },
            data: {
                body: body
            }
        }, function(ret, err) {
            if (ret) {
                moneyOfCount = ret.money;
                $api.setStorage('moneyOfCount', moneyOfCount);
                // alert( JSON.stringify( ret ) );
            } else {
                // alert( JSON.stringify( err ) );
            }
        });

    }

    window.postMon = function(id, tieid) {
            ajaxOfjifen();
            moneyOfCount = $api.getStorage('moneyOfCount');
            api.prompt({
                title: '剩余' + moneyOfCount,
                buttons: ['确定', '取消']
            }, function(ret, err) {
                if (ret) {
                    var index = ret.buttonIndex;
                    var text = ret.text;
                    var token = $api.getStorage('token');
                    if (index == 1) {
                        var body = {
                            token: token,
                            tieid: tieid,
                            id: id,
                            text: text
                        };
                        api.ajax({
                            url: 'http://www.mk517.com/webApi/reward.php',
                            method: 'post',
                            dataType: 'json',
                            "headers": {
                                "Content-Type": "application/json"
                            },
                            data: {
                                body: body
                            }
                        }, function(ret, err) {
                            if (ret) {
                                if (ret.info == "true") {
                                    api.alert({
                                        msg: '打赏成功！',
                                    }, function(ret, err) {});
                                    ajaxOfjifen();
                                    $api.rmStorage('moneyOfCount');
                                } else {
                                    api.alert({
                                        msg: '您的积分不足！',
                                    }, function(ret, err) {});

                                }
                                //alert( JSON.stringify( ret ) );
                            } else {
                                // alert( JSON.stringify( err ) );
                            }
                        });

                    }
                    //  alert( JSON.stringify( ret ) );
                } else {
                    //  alert( JSON.stringify( err ) );
                }
            });
        }
        /*  打赏功能end */
    window.Mine = function() {
        var token = $api.getStorage('token');
        if (token == '') {
            api.alert({
                msg: '您还未登录，请先登录！'
            })
        }
        var body = {
            categry: 2,
            token: token
        }
        cancelAjax(body);
        //refresh(cancelAjax, body);
        changeIndexFrame(2);
    }

    function cancelAjax(body) {
        api.ajax({
            url: 'http://www.mk517.com/webApi/youquans.php',
            method: 'post',
            dataType: 'json',
            "headers": {
                "Content-Type": "application/json"
            },
            data: {
                body: body
            }
        }, function(ret, err) {
            if (ret) {
                if (ret.length > 0) {
                    $api.byId('content').innerHTML = '';
                    for (var i = 0; i < ret.length; i++) {
                        data = ret[i];
                        str = 'change_status' + i;
                        arr.push(data.id);
                        arr2.push(data.tieid);
                        laud = 'laud' + i;
                        (data.post_picture != null && data.post_picture != '') ? (imgAddress = '<div style="width:auto;height:auto;" onclick="openPostInfo(' + data.tieid + ',' + data.id + ')"><img src="' + data.post_picture + '" id="picture1" style="width:100%;height:auto;max-width:100%;max-height:100%;"></div>') : imgAddress = '';
                        $api.byId('content').innerHTML +=
                            '<div class="aui-card-list">' +
                            '<div class="aui-card-list-header aui-card-list-user aui-border-b">' +
                            '<div class="aui-card-list-user-avatar">' +
                            '<img id="post_user_avater" src="' + data.post_user_avater + '" class="aui-img-round" />' +
                            '</div>' +
                            '<div class="aui-card-list-user-name">' +
                            '<div id="post_username">' + data.post_username + '</div>' +
                            '<span class="aui-iconfont aui-icon-close" onclick="cancel(' + i + ',' + data.tieid + ');"></span>' +
                            '</div>' +
                            '</div>' +
                            '<div class="aui-card-list-content-padded">' +
                            '<p id="post_goods_content" onclick="openPostInfo(' + data.tieid + ',' + data.id + ')">' + data.post_goods_content + '</p>' +
                            imgAddress +
                            '</div>' +
                            '<div class="aui-card-list-footer aui-border-t">' +
                            '<div id="post_note_count"><i class="aui-iconfont aui-icon-note" id="note" onclick="postComment(' + i + ')">' + data.post_note_count + '</i></div>' +
                            '<div id="post_laud_count"><i id="' + laud + '" class="aui-iconfont aui-icon-laud" onclick="postLaud(' + i + ',' + data.tieid + ')">' + data.post_laud_count + '</i></div>' +
                            '<div id="post_star_count"><i class="aui-iconfont aui-icon-share" onclick="sharePost(' + i + ');"></i>' +
                            '</div>' +
                            '<div id="post_star_count"><i class="icon iconfont icon-hongbao" onclick="postMon(' + data.id + ',' + data.tieid + ')">0</i>' +
                            '</div>' +
                            '</div></div></div>';

                    }
                }
            } else {
                //alert(JSON.stringify(err));
            }
        });
    }

    /* 帖子删除功能 */
    window.cancel = function(i, tieid) {
        var token = $api.getStorage('token');
        var body = {
            token: token,
            tieid: tieid
        };
        api.confirm({
            msg: '确认删除？',
            buttons: ['确认', '取消']
        }, function(ret, err) {
            if (ret) {
                if (ret.buttonIndex == 1) {
                    api.ajax({
                        url: 'http://www.mk517.com/webApi/deletedynamic.php',
                        method: 'post',
                        dataType: 'json',
                        "headers": {
                            "Content-Type": "application/json"
                        },
                        data: {
                            body: body
                        }
                    }, function(ret, err) {
                        if (ret) {
                            if (ret.info == 'true') {
                                api.alert({
                                    msg: '删除成功'
                                }, function(ret, err) {
                                    if (ret) {
                                        Mine();
                                    }
                                });

                            }
                            // alert( JSON.stringify( ret ) );
                        } else {
                            // alert( JSON.stringify( err ) );
                        }
                    });
                }
            } else {
                //alert( JSON.stringify( err ) );
            }
        });


    }
}
